﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using TrelloNet;
using System.Configuration;
using System.Collections.Specialized;
using System.Reflection;
using System.Diagnostics;

namespace SprintTrelloAutomater
{
    /**********************************************************************
      https://github.com/dillenmeister/Trello.NET/wiki
      https://github.com/dillenmeister/Trello.NET/wiki/Examples
    **********************************************************************/


    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        ITrello trello;
        NameValueCollection settings = ConfigurationManager.AppSettings;
        string RoydanOrgID;

        public MainWindow()
        {
            InitializeComponent();

            string versionString = "Roydan Sprint Board Automater v" + FileVersionInfo.GetVersionInfo(Assembly.GetExecutingAssembly().Location).FileVersion;

            string trelloKey = settings["TrelloKey"];
            if (string.IsNullOrEmpty(trelloKey))
            {
                ShowError("TrelloKey is missing in the app configuration");
                return;
            }

            string trelloAPIKey = settings["TrelloAPIKey"];
            if (!string.IsNullOrEmpty(trelloAPIKey))
                txtAuthKey.Text = trelloAPIKey;

            RoydanOrgID = settings["RoydanOrgID"];
            if (String.IsNullOrEmpty(RoydanOrgID))
            {
                // TODO - prompt w/ list of boards to select a roydan board
            }

            trello = new Trello(trelloKey);
            webBrowser.Navigate(trello.GetAuthorizationUrl(versionString, Scope.ReadWrite));
        }

        private void ShowError(string error)
        {
            txtMessages.Text = error;
            webBrowser.Visibility = System.Windows.Visibility.Hidden;
        }

        private void Button_Click_1(object sender, RoutedEventArgs e)
        {
            trello.Authorize(txtAuthKey.Text);

            var allBoards = trello.Boards.ForMe(BoardFilter.Open);

            var boardNameandOrg = from board in allBoards
                                  join org in trello.Organizations.ForMe() on board.IdOrganization equals org.Id
                                  select new { Name = board.Name, OrgID = org.Id, OrgName = org.Name, OrgDispName = org.DisplayName };

            Board toCopy = allBoards.First<Board>();        // TODO: actually pick the board here

            var copyLabels = toCopy.LabelNames;
            var copyPrefs = toCopy.Prefs;
            var copyLists = trello.Lists.ForBoard(toCopy);
            var copyMembers = trello.Members.ForBoard(toCopy);

            //// Testing
            ////var activeBoards = from Board b2 in trello.Boards.ForMe()
            ////                   where b2.Closed == false
            ////                   select b2;

            ////foreach (TrelloNet.Board b in activeBoards)
            ////{
            ////    Console.WriteLine(b.IdOrganization);
            ////}

            TrelloNet.Board b = new TrelloNet.Board();

            b = trello.Boards.Add(txtBoardName.Text);
            //b.IdOrganization = RoydanOrgID;
            foreach (TrelloNet.List li in trello.Lists.ForBoard(b))
            {
                // Close out the default lists
                li.Closed = true;
            }

            trello.Lists.Add("List 1", b);
            trello.Lists.Add("List 2", b);

            b.LabelNames.Add(TrelloNet.Color.Red, "Bug");
            b.Name = "test name";
            b.Closed = false;
        }

        private void Button_Click_2(object sender, RoutedEventArgs e)
        {
            // TODO: Save the settings here

            //settings.Add("TrelloAPIKey", txtAuthKey.Text);
        }
    }
}
